package vnetlab.elements;

import java.io.Serializable;
import javax.swing.JOptionPane;
import vnetlab.manager.Status;


public class Element implements Serializable{
    
    private String name;
    private String label;
    private String ident;
    private String type;
    private String template;
    private Interface [] interfaces;
    private Status status;
    private boolean created;
    private boolean terminalOpened;


    public Element(){
        this.name = "";
        this.label = "";
        this.ident = "";
        this.type = "";
        this.template = "";
//        interfaces = new Interface[1];
//        for(int i=0; i < interfaces.length; i++)
//            interfaces[i] = new Interface(i, "", false);
        status = Status.STOP;
        created = false;
    }
    
    public Element(String name, String id, String type, int nroInterfaces, String template) {
        
        this.name = name;
        this.label = this.name;
        this.ident = id;
        this.type = type;
        this.template = template;
        interfaces = new Interface[nroInterfaces];
        
        for(int i=0; i < interfaces.length; i++)
            interfaces[i] = new Interface(i,false);
        status = Status.STOP;
        created = false;
    }

    /*********** GETS AND SETS ******************************/

    public boolean isCreated() {
        return created;
    }
    
    public void setCreated(boolean created) {
        this.created = created;
    }

   

    public void setInterfaces(Interface[] interfaces) {
        this.interfaces = interfaces;
    }

    public Interface[] getInterfaces() {
        return interfaces;
    }
    
    public void setIdent(String id) {
        this.ident = id;
    }

    public String getIdent() {
        return ident;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }


    public void setType(String type) {
        this.type = type;
    }
    
    public String getType() {
        return type;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Status getStatus() {
        return status;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public String getTemplate() {
        return template;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public void setTerminalOpened(boolean terminalOpened) {
        this.terminalOpened = terminalOpened;
    }

    public boolean isTerminalOpened() {
        return terminalOpened;
    }

    /*********** OUTROS MÉTODOS ******************************/
    @Override
    public String toString() {
        return this.label;
    }

    public Interface getInterface(int id){
        return interfaces[id];
    }

    public void addInterface(Interface nic, int id) {
        
        interfaces[id] = nic;
    }

    public void connect(int id, String name, String type) {
        interfaces[id].setDestine(name);
        interfaces[id].setConnected(true);
        interfaces[id].setDestineType(type);
    }

    public void disconnect(int id,String name, String type){
        interfaces[id].setDestine(name);
        interfaces[id].setConnected(false);
        interfaces[id].setDestineType(type);
    }

    public String getConectedType(int id) {
        if (interfaces[id].isConnected()) {
            return interfaces[id].getDestineType();
        } else {
            return " ";//Tipo nulo
        }
    }

    public String conectedWho(int id) {
        if (id<=interfaces.length && interfaces[id].isConnected()) {
            return interfaces[id].getDestine();
        } else {
            return null;
        }
    }

    public boolean isConected(int id) {
        if(interfaces[id] == null)
            return false;
        
        return interfaces[id].isConnected();
    }

    public int numberOfNIC() {
        return interfaces.length;
    }

    //funciona mas eh gambiarra!rever.
     public boolean checkInterfaceAvailable(){
        for(Interface i :interfaces)
            if(!i.isConnected())
                return true;
        JOptionPane.showMessageDialog(null, "The element "+getName()+" has "+numberOfNIC()+" network interfaces and all of them are busy at the moment.");
        return false;
    }

     public int idTarget(String target){
         for(Interface i :interfaces){
             if(i.isConnected() && i.getDestine().contains(target))
                 return i.getIdent();
         }
         return -1;
     }
}